package com.zyf.chapter02.structure.proxy;

import com.zyf.chapter01.practice.v3.MetricsCollector;
import com.zyf.chapter01.principle.practice.RequestInfo;

/**
 * 静态代理
 * 如果原始类没有定义接口，并且原始类代码并不是我们开发维护的，那就没办法使用接口了。
 * 方案：使用继承的方式
 * @author yifeng
 */
public class V2UserControllerProxy extends UserController {
    private MetricsCollector metricsCollector;

    public V2UserControllerProxy() {
        this.metricsCollector = new MetricsCollector();
    }

    @Override
    public UserVo login(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();

        UserVo userVo = super.login(telephone, password);

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        RequestInfo requestInfo = new RequestInfo("login", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);
        return null;
    }

    @Override
    public UserVo register(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();

        UserVo userVo = super.register(telephone, password);

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        RequestInfo requestInfo = new RequestInfo("register", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);
        return null;
    }

    public static void main(String[] args) {
        // UserControllerProxy 使用举例
        UserController userController = new V2UserControllerProxy();
    }
}
